package com.dd.stack.redis.controller;

import com.dd.stack.redis.service.inner.impl.LeaderboardServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @Author liuxianmeng
 * @CreateTime 2025/1/31 23:19
 * @Description 排行榜
 */
@RestController
@Api(tags = "排行榜")
@RequiredArgsConstructor
@RequestMapping("/leaderboard")
public class LeaderboardController {

    @Autowired
    private LeaderboardServiceImpl leaderboardService;

    @PostMapping("/add")
    @ApiOperation("增加或更新用户分数")
    public ResponseEntity<String> addScore(@RequestParam Long userId, @RequestParam double score) {
        leaderboardService.addOrReplaceScore(userId, score);
        return ResponseEntity.ok("Score added/updated successfully");
    }

    @ApiOperation("获取用户排名")
    @GetMapping("/rank/{userId}")
    public ResponseEntity<Long> getRank(@PathVariable Long userId) {
        Long userRank = leaderboardService.getUserRank(userId);
        return ResponseEntity.ok(userRank);
    }

    @ApiOperation("获取前N名用户")
    @GetMapping("/top/{count}")
    public ResponseEntity<Set<Long>> getTopUsers(@PathVariable int count) {
        Set<Long> topUsers = leaderboardService.getTopRanks(count);
        return ResponseEntity.ok(topUsers);
    }

    @ApiOperation("增加用户分数")
    @PostMapping("/increment")
    public ResponseEntity<String> incrementScore(@RequestParam Long userId, @RequestParam double increment) {
        leaderboardService.addOrIncreScore(userId, increment);
        return ResponseEntity.ok("Score incremented successfully");
    }
}
